package com.sihan.framework.etm.controller;

import com.framework.annotation.BusinessLog;
import com.framework.annotation.ParamParser;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.common.PageRequest;
import com.framework.model.PageResult;
import com.sihan.framework.etm.dto.RoleAddDTO;
import com.sihan.framework.etm.dto.RolePermissionDTO;
import com.sihan.framework.etm.dto.RoleUpdateDTO;
import com.sihan.framework.etm.entity.Role;
import com.sihan.framework.etm.service.RoleService;
import com.sihan.framework.etm.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/system/v0/role")
public class AdminRoleController {

  @Autowired
  private RoleService roleService;

  @ApiOperation("分页获得角色信息")
  @GetMapping("/page")
  @BusinessLog(value = "分页检索角色信息")
  @RequiresPermissions("sys:manager:role:menu")
  public ApiResult<?> pageRoles(PageRequest pageRequest) {
    PageResult<RoleVO> result = roleService.pageRoleInfo(pageRequest);
    return ApiResult.success(result);
  }

  @ApiOperation("获得简单的角色信息(没有权限信息)")
  @GetMapping("/list")
  @BusinessLog(value = "获得简单的角色信息")
  @RequiresPermissions("sys:manager:role:menu")
  public ApiResult<List<RoleVO>> listRoles() {
    List<RoleVO> roles = roleService.getRoleList();
    return ApiResult.success(roles);
  }

  @ApiOperation("添加角色信息")
  @PostMapping("/add")
  @BusinessLog(value = "添加角色")
  public ApiResult<?> addRole(@Valid @RequestBody @ParamParser RoleAddDTO dto) {
    roleService.addRole(dto.getName(), dto.getIdentify(),
        dto.getRemarks());
    return ApiResult.success();
  }

  @ApiOperation("修改角色信息")
  @PostMapping("/update")
  @BusinessLog(value = "修改角色")
  public ApiResult<?> updateRole(@Valid @RequestBody @ParamParser RoleUpdateDTO dto) {
    Role role = new Role();
    BeanUtils.copyProperties(dto, role);
    roleService.updateRole(role);
    return ApiResult.success();
  }
  
  @ApiOperation("分配权限")
  @PostMapping("/menu/save")
  @BusinessLog(value = "分配权限")
  public ApiResult<?> saveRolePermission(@Valid @ApiParam(name = "dto", value = "分配权限数据", required = true) @RequestBody RolePermissionDTO dto) {
    roleService.saveRolePermission(dto.getId(), dto.getPermissionIds());
    return ApiResult.success();
  }

  @ApiOperation("删除角色信息")
  @PostMapping("/delete")
  @BusinessLog("删除角色")
  public ApiResult<?> deleteRole(@Valid @RequestBody @NotNull @ParamParser List<String> roleIds) {
    roleService.deleteRolesByIds(roleIds);
    return ApiResult.success();
  }
}
